package org.lanqiao.tjut.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.lanqiao.tjut.bean.CourseBean;
import org.lanqiao.tjut.bean.EduScoreAllInfoViewBean;
import org.lanqiao.tjut.bean.UsersBean;
import org.lanqiao.tjut.db.DBDriverFactory;
import org.lanqiao.tjut.myUtils.MyUtils;

/**
 * @author Administrator
 *
 */
public class StuInfoManagerDao {

	/**
	 * 根据教师id获取该教师所教授课程的信息列表
	 * 
	 * @param teaId
	 *            教师id
	 * @return 教授课程的信息列表
	 */
	public List<CourseBean> getCourseInfoByTeaId(int teaId) {

		// 查询数据库的sql语句
		String strSql = "SELECT * FROM EDUCOURSES WHERE COURSEID IN (SELECT COURSEID FROM EDUTEA_COURSES WHERE TEAID=?)";
		List<CourseBean> lstCourse = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanListHandler<CourseBean>(CourseBean.class), new Object[] { teaId });
		return lstCourse;
	}

	/**
	 * 获取老师教授课程的学生信息
	 * 
	 * @param teaId
	 *            教师id
	 * @param courseId
	 *            课程id
	 * @return 学生信息
	 */
	public List<EduScoreAllInfoViewBean> getStuInfoBy4Grade(int teaId, int courseId) {
		// 查询数据库的sql语句
		String strSql = "SELECT * FROM EDUSCOREALLINFOVIEW WHERE 1=1 AND TEAID=? AND COURSEID=?";
		List<EduScoreAllInfoViewBean> lstStuInfo4Grade = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanListHandler<EduScoreAllInfoViewBean>(EduScoreAllInfoViewBean.class),
				new Object[] { teaId, courseId });
		return lstStuInfo4Grade;
	}

	public int updateStuScore(String strParasScores) {
		String strSql = "UPDATE EDUSTU_SCORES SET SCORE = ? WHERE SCOREID=?";
		int[] i = DBDriverFactory.getDBDriverInstance().batch(strSql, paseStrParas2Objects(strParasScores));
		return 1;
	}

	/**
	 * 将字符串转换为批处理需要的二维数组参数的格式
	 * 
	 * @param strParasScores
	 *            参数字符串
	 * @return 二维数组参数
	 */
	private Object[][] paseStrParas2Objects(String strParasScores) {
		Object[][] arrParas = null;
		if (strParasScores != null && !strParasScores.trim().equals("")) {
			// 解析字符串
			String[] strArr = strParasScores.split(",");
			arrParas = new Object[strArr.length][2];
			for (int i = 0; i < strArr.length; i++) {
				String[] strSubArr = strArr[i].split(":");
				if (strSubArr.length == 2) {
					// 参数数组
					arrParas[i] = new Object[] { strSubArr[1], strSubArr[0] };
				}/*else{
					arrParas[i] = new Object[] { null, strSubArr[0] };
				}*/
			}
		}
		MyUtils.logger_console.debug(arrParas);
		return arrParas;
	}
}
